package com.sinacom.ibuilding.action;



import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.sinacom.ibuilding.beans.Member;
import com.sinacom.ibuilding.beans.News;
import com.sinacom.ibuilding.beans.Role;
import com.sinacom.ibuilding.service.NewsService;
import com.sinacom.ibuilding.util.Constants;
import com.sinacom.ibuilding.util.Helper;
import com.sinacom.ibuilding.util.PageModel;

public class NewsAction {
	
	private NewsService newsService = new NewsService();
	private int page = 1;
	private PageModel newsAll;
	private PageModel newsLaw;
	private PageModel newsInfo;
	private News news;
	
	private File attach;
	private String attachFileName;
	private String attachContentType;
	
	private String filePath;
	private String id;
	
	private String category;
	
	private News preNews;
	private News nextNews;
	
	private List charList = new ArrayList();
	public String view()throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();  
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		String accessRes = role.getAccessRes();
		if(!StringUtils.isEmpty(accessRes)){
			String inAccess = "";
			String[] accesss = accessRes.split(",");
			for (int i = 0; i < accesss.length; i++) {
				if(StringUtils.isEmpty(inAccess)){
					inAccess=("'"+accesss[i]+"'");
				}else{
					inAccess=(inAccess+",'"+accesss[i]+"'");
				}
				
			}
			if(!StringUtils.isEmpty(category)){
			   if(inAccess.indexOf(category) != -1){
			       newsAll = newsService.getNewsCategory("('"+category+"')",null, page);
			   }else{
			       request.setAttribute("msg", "您查看该模块的权限");
		           return "error";
			   }
			}else{
			    newsAll = newsService.getNewsCategory("("+inAccess+")",null, page);
			}
			Helper.newList(newsAll.getList());
		//	newsLaw = newsService.getNewsCategory("("+inAccess+")",new Integer(Constants.NEWS_TYPE_LAW), page);
		//	newsInfo = newsService.getNewsCategory("("+inAccess+")",new Integer(Constants.NEWS_TYPE_INFO), page);
		}
		return "view";
	}
	
	public String publish()throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest(); 
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		if(role.getNewsMang() != null && role.getNewsMang().booleanValue()){
			String chargeRes = role.getChargeRes();
			if(!StringUtils.isEmpty(chargeRes)){
				String[] chargeResList = chargeRes.split(",");
				for (int i = 0; i < chargeResList.length; i++) {
					String cateName = Constants.getCategoryName(chargeResList[i]);
					Map map = new HashMap();
					map.put("id", chargeResList[i]);
					map.put("name", cateName);
					charList.add(map);
				}
			}
			
			return "publish";
		}else{
			request.setAttribute("msg", "您没有管理权限");
			return "error";
		}
	}
	
	public String save()throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8"); 
        PrintWriter pw = null;
        pw = response.getWriter();
		HttpServletRequest request = ServletActionContext.getRequest(); 
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		member.setRole(role);
		if(role.getNewsMang() != null && role.getNewsMang().booleanValue()){
			newsService.saveNews(member,news); 
			
	        Map map = new HashMap();  
	        map.put("success", "true");
	        map.put("msg", "发布成功");
	        pw.print(JSONObject.fromObject(map));
			return null;
		}else{
			Map map = new HashMap();  
	        map.put("success", "false");
	        map.put("msg", "您没有管理权限");
	        pw.print(JSONObject.fromObject(map));
			return null;
		}
		
	}
	
	public String delete()throws Exception{
        HttpServletRequest request = ServletActionContext.getRequest(); 
        Member member = (Member) request.getSession().getAttribute("member");
        Role role = member.getRole();
        News news = newsService.getNewsById(id);
        if(role.getNewsMang() != null && role.getNewsMang().booleanValue() && role.getChargeRes().indexOf(news.getCategory())!=-1){
          newsService.deleteNews(id);
          
          HttpServletResponse response = ServletActionContext.getResponse();
          response.sendRedirect("news!manage.do");
          return null;
        }else{
            return "error";
        }
        
    }
	
	public String manage()throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		if(role.getNewsMang() != null && role.getNewsMang().booleanValue()){
			String chargeRes = role.getChargeRes(); 
			if(!StringUtils.isEmpty(chargeRes)){
				newsAll = newsService.getNewsCategory(getInCharge(chargeRes),null, page);
			}
			Helper.newList(newsAll.getList());
			return "manage";
		}else{
			request.setAttribute("msg", "您没有管理权限");
			return "error";
		}
		
		
	}

	private String getInCharge(String chargeRes){
		String inCharge = "";
		String[] accesss = chargeRes.split(",");
		for (int i = 0; i < accesss.length; i++) {
			if(StringUtils.isEmpty(inCharge)){
				inCharge=("'"+accesss[i]+"'");
			}else{
				inCharge=(inCharge+",'"+accesss[i]+"'");
			}
			
		}
		return "("+inCharge+")";
	}
	
	public String read()throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		news = newsService.getNewsById(id);
		preNews = newsService.getPreNews(id,getInCharge(role.getAccessRes()));
		nextNews = newsService.getNextNews(id,getInCharge(role.getAccessRes()));
		return "read";
	}
	public String edit()throws Exception{
		news = newsService.getNewsById(id);
		
		HttpServletRequest request = ServletActionContext.getRequest(); 
		Member member = (Member) request.getSession().getAttribute("member");
		Role role = member.getRole();
		if(role.getNewsMang() != null && role.getNewsMang().booleanValue() && role.getChargeRes().indexOf(news.getCategory())!=-1){
			String chargeRes = role.getChargeRes();
			if(!StringUtils.isEmpty(chargeRes)){
				String[] chargeResList = chargeRes.split(",");
				for (int i = 0; i < chargeResList.length; i++) {
					String cateName = Constants.getCategoryName(chargeResList[i]);
					Map map = new HashMap();
					map.put("id", chargeResList[i]);
					map.put("name", cateName);
					charList.add(map);
				}
			}
			return "publish";
		}else{
			request.setAttribute("msg", "您没有管理权限");
			return "error";
		}
	}
	
	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	
	
	

	public File getAttach() {
		return attach;
	}

	public void setAttach(File attach) {
		this.attach = attach;
	}

	public String getAttachFileName() {
		return attachFileName;
	}

	public void setAttachFileName(String attachFileName) {
		this.attachFileName = attachFileName;
	}

	public String getAttachContentType() {
		return attachContentType;
	}

	public void setAttachContentType(String attachContentType) {
		this.attachContentType = attachContentType;
	}

	
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	

	
	

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public PageModel getNewsAll() {
		return newsAll;
	}

	public void setNewsAll(PageModel newsAll) {
		this.newsAll = newsAll;
	}

	public PageModel getNewsLaw() {
		return newsLaw;
	}

	public void setNewsLaw(PageModel newsLaw) {
		this.newsLaw = newsLaw;
	}

	public PageModel getNewsInfo() {
		return newsInfo;
	}

	public void setNewsInfo(PageModel newsInfo) {
		this.newsInfo = newsInfo;
	}

	public static void main(String[] args) {
		System.out.println(DigestUtils.md5Hex("111111"));
	}

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

	public List getCharList() {
		return charList;
	}

	public void setCharList(List charList) {
		this.charList = charList;
	}

	public News getPreNews() {
		return preNews;
	}

	public void setPreNews(News preNews) {
		this.preNews = preNews;
	}

	public News getNextNews() {
		return nextNews;
	}

	public void setNextNews(News nextNews) {
		this.nextNews = nextNews;
	}
	
		
	
}
